package cn.edu.zut.soft.student.controller.admin.course;

import cn.edu.zut.soft.student.bean.ResultBean;
import cn.edu.zut.soft.student.model.Course;
import cn.edu.zut.soft.student.service.course.CourseBo;
import cn.edu.zut.soft.student.service.course.vo.CourseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/20 11:43
 */

@RestController
@RequestMapping("/admin/api/course")
public class CourseController {
    @Autowired
    private CourseBo courseBo;

    @PostMapping("")
    public ResultBean<Integer> addCourse(Course course) {
        return new ResultBean<>(courseBo.addCourse(course));
    }

    @PostMapping("/import")
    public ResultBean<Integer> importStudents(MultipartFile file) throws Exception {
        return new ResultBean<>(courseBo.importCourses(file));
    }

    @DeleteMapping("/{courseid}")
    public ResultBean<Integer> deleteCourse(@PathVariable("courseid") String courseid) {
        return new ResultBean<>(courseBo.deleteCourse(courseid));
    }

    @DeleteMapping("/list")
    public ResultBean<Integer> deleteCourses(String allCourseIds) {
        String[] ids = allCourseIds.substring(1, allCourseIds.length() - 1).split(",");
        for (String id : ids) {
            courseBo.deleteCourse(id);
        }
        return new ResultBean<>();
    }

    @PutMapping("")
    public ResultBean<Integer> updateCourse(Course course) {
        return new ResultBean<>(courseBo.updateCourse(course));
    }

    @GetMapping("/{courseid}")
    public ResultBean<Course> queryCourse(@PathVariable("courseid") String courseid) {
        return new ResultBean<>(courseBo.queryCourseById(courseid));
    }

    @GetMapping("")
    public ResultBean<List<Course>> queryCourses(CourseQuery courseQuery) {
        return new ResultBean<>(courseBo.queryCourses(courseQuery));
    }
}
